#include <lpc23xx.h>
#include "LCD.h"

void FIQ_Handler (void)__irq; 
unsigned int flasher = 0x0000000; 	

int main (void)
{
    
unsigned int result,delay; //
	/* Reset all GPIO pins to default: primary function */
    PINSEL0 = 0x00000000;
    PINSEL1 = 0x00000000;
    PINSEL2 = 0x00000000;
    PINSEL3 = 0x00000000;
    PINSEL4 = 0x00000000;
    PINSEL5 = 0x00000000;
    PINSEL6 = 0x00000000;
    PINSEL7 = 0x00000000;
    PINSEL8 = 0x00000000;
    PINSEL9 = 0x00000000;
    PINSEL10 = 0x00000000;
    
    IODIR0 = 0x00000000;
    IODIR1 = 0x00000000;
	IOSET0 = 0x00000000;
    IOSET1 = 0x00000000;
    
    FIO0DIR = 0x00000000;
    FIO1DIR = 0x00000000;
    FIO2DIR = 0x00000000;
    FIO3DIR = 0x00000000;
    FIO4DIR = 0x00000000;
    
    FIO0SET = 0x00000000;
    FIO1SET = 0x00000000;
    FIO2SET = 0x00000000;
    FIO3SET = 0x00000000;
    FIO4SET = 0x00000000;					
	lcd_init();
	lcd_clear();
	set_cursor(0,1);
	lcd_print("Clock");

    
    SCS = 0x00000020;
    while (!( SCS &0x00000040))
        {
            ;
        }
    
	 PINSEL4 	    = 0x00100000;						//Enable the EXTINT1 interrupt
    EXTMODE         = 0x00000001;                       //Set INT0 to edge sensetive mode
    EXTPOLAR        = 0x00000001;                       //Select rising edge
    EXTINT          = 0x00000001;                       //Clear the interrupt flags
    VICIntSelect    = 0x00004000;
    VICIntEnable    = 0x00004000;	
    
    
	FIO2DIR = 0x000000FF;
    FIO2MASK = 0xFFFFFF00;
    FIO2CLR = 0x000000FF;
    
    while(1)
    {
    FIO2CLR = 0x000000FF;
    
    
  

    for(flasher =1;flasher<0x00000100;flasher = flasher <<1)
    {
       FIO2SET = flasher; 
        for(delay = 0;delay<0x100000;delay++)				  	//simple delay loop
        {
        ;
        }
    }
  
    }
    
return (0);    
}


void FIQ_Handler (void)	__irq
{
  
unsigned char debounce; 
            
if(CLKSRCSEL == 0x00000001)
                {
                  lcd_clear();
                  lcd_print("RTC" );
                  CLKSRCSEL = 0x00000002; 		// 32 kHz
				  set_cursor(0,1);
				  lcd_print("Clock");                 
                }
	else if (CLKSRCSEL == 0x00000000)
    	            {
        	          lcd_clear();
            	      lcd_print("Main" );
                  	  CLKSRCSEL = 0x00000001;	// 12 MHz
				      set_cursor(0,1);
					  lcd_print("Clock");
                	}
	else if (CLKSRCSEL == 0x00000002)
    	            {
        	          lcd_clear();
                      lcd_print("Iternal" );
                      CLKSRCSEL = 0x00000000;	// 4 MHz
				      set_cursor(0,1);
					  lcd_print("Clock");
                	}
         
        
for(debounce = 0;debounce<0xFE;debounce++) //Debounce the switch
  {
    ;
  }


EXTINT  		= 0x00000001;			    //Clear the peripheral interrupt flag
}
